<template>
  <BasicModal
    :title="getTitle"
    :width="400"
    :canFullscreen="false"
    :centered="true"
    :destroyOnClose="true"
    @register="registerModal"
    @ok="handleSubmit"
  >
    <BasicForm @register="registerForm" :model="model" />
  </BasicModal>
</template>

<script lang="ts" name="BankModal" setup>
import { ref, computed, unref, reactive } from "vue";
import { BasicModal, useModalInner } from "/@/components/Modal";
import { BasicForm, useForm } from "/@/components/Form/index";
import { useMessage } from "/@/hooks/web/useMessage";
import { visitorSave, visitorById } from "/@/views/shop/orders/orders.api";
import { inputDefaultValue } from "ant-design-vue/es/vc-input/inputProps";

const $message = useMessage();

// 声明Emits
const emit = defineEmits(['success', 'register']);
//表单modal
const model = reactive<any>({})

//表单配置
const [registerForm, {resetFields, setFieldsValue, updateSchema, validate}] = useForm({
  schemas: [
    { label: "", field: "id", component: "Input", show: false },
    { label: "", field: "orderSn", component: "Input", show: false },
    { label: "游客姓名", field: "name", component: "Input", required: true, componentProps: { autoComplete: "off" } },
    { label: "证件类型", field: "type", component: "Select", required: true,
      componentProps: ({ formActionType }) => {
        return {
          options: [
            { label: '身份证', value: '1' },
            { label: '军官证', value: '2' },
            { label: '护照', value: '3' },
            { label: '港澳通行证', value: '4' },
            { label: '台湾通行证', value: '5' },
            { label: '台胞证', value: '6' },
            { label: '回乡证', value: '7' },
          ],
          getPopupContainer: () => document.body,
        }
      },
      defaultValue: '1'
    },
    { label: "证件号码", field: "number", component: "Input", required: true, componentProps: { autoComplete: "off" } },
    { label: "联系电话", field: "tel", component: "Input", required: true, componentProps: { autoComplete: "off" } },
  ],
  labelWidth: 90,
  baseColProps: {span: 22},
  showActionButtonGroup: false,
});

const isUpdate = ref(true);

//表单赋值
const [registerModal, {setModalProps, closeModal}] = useModalInner(async (data) => {
  setModalProps({confirmLoading: false});
  isUpdate.value = !!data?.isUpdate;
  await resetFields()
  let record = {}

  if (isUpdate.value) {
    record = await visitorById({id: data?.id});
  } else {
    record = { orderSn: data.orderSn}
  }

  await setFieldsValue({
    ...record
  })
});

//标题
const getTitle = computed(() => (!unref(isUpdate) ? '新增' : '编辑'));

//表单提交事件
async function handleSubmit() {
  try {
    setModalProps({confirmLoading: true});
    const values = await validate();
    await visitorSave(values).then((res) => {
      if (res.success) {
        $message.createMessage.success(res.message);
        //关闭弹窗
        closeModal();
        emit("success")
      } else {
        $message.createMessage.warning(res.message);
      }
    });
  } catch (e) {
    console.log(e);
  } finally {
    setModalProps({confirmLoading: false});
  }
}

</script>

<style lang="less" scoped>
:deep(.ant-upload.ant-upload-select-picture-card) {
  width: 360px!important
}
</style>
